package demo04;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author: WangPeng
 * @createTime: 2022/11/04 16:28
 * @description:
 * @version: v1.0
 */
//构建方法引用  类名::new       (args) -> new 类名(args)
//lambda表达式中只有一条语句，而且这条数据是实例一个对象，调用构造方法，
// 而且构造函数的参数和lambda表达式的参数一致。
public class Test05 {
    public static void main(String[] args) {
        //调用有参的构造函数
        Function<String,Animal> function1 = (name)->new Animal(name);
        Animal animal1 = function1.apply("dog");
        System.out.println("animal1 = " + animal1);

        Function<String,Animal> function2 = Animal::new;
        Animal animal2 = function2.apply("cat");
        System.out.println("animal2 = " + animal2);
        //调用无参的构造函数
        Supplier<Animal> supplier1 = ()->new Animal();
        System.out.println(supplier1.get());
        Supplier<Animal> supplier2 = Animal::new;
        System.out.println(supplier2.get());
    }
}
class Animal {
    private String name;

    public Animal() {
    }

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                '}';
    }
}
